<template>
  <div class="table-page-search-wrapper">
    <a-card>
      <a-form layout="vertical">
        <a-row :gutter="48">
          <!-- <a-col :md="6" :sm="12">
            <a-form-item
              :label="$t('key.kkyx')"
            >
              <a-select :allowClear="true" v-model="queryParam.kclb" @change="lbChange">
                <a-select-option v-for="i in TYPE0111" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col> -->

          <a-col :md="6" :sm="12">
            <a-form-item
              :label="$t('key.kkxn')"
            >
              <a-select :allowClear="true" v-model="queryParam.kkxn">
                <a-select-option v-for="i in TYPE0125" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item
              :label="$t('key.kkxq')"
            >
              <a-select :allowClear="true" v-model="queryParam.kkxqm">
                <a-select-option v-for="i in TYPE0072" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item
              :label="$t('key.kkyx')"
            >
              <a-select allowClear v-model="queryParam.kkyxm" showSearch :filterOption="filterOption">
                <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh+'-'+i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item
              :label="$t('key.kclb')"
            >
              <a-select :allowClear="true" v-model="queryParam.kclbm">
                <a-select-option v-for="i in TYPE0359" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <!-- <a-col :md="6" :sm="12">
            <a-form-item label="课程类别" :labelCol="{span: 7,offset: 1}" :wrapperCol="{span: 15, offset: 1}">
              <a-select :allowClear="true" v-model="queryParam.kclb" @change="lbChange">
                <a-select-option v-for="i in TYPE0111" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col> -->
        </a-row>
        <a-row>
          <span class="table-page-search-submitButtons">
            <a-button type="primary" @click="$refs.table.refresh(true)">{{ $t('button.query') }}</a-button>
            <a-button type="primary" @click="handleExport" style="margin:0 15px">{{ $t('button.export') }}</a-button>
            <a-button @click="() => queryParam = {}">{{ $t('button.reset') }}</a-button>
            <a-button style="margin-left: 15px;" type="primary" @click="expoetSub">导出课表</a-button>
            <!-- <a @click="advanced = !advanced" style="margin-left: 8px">
              {{ advanced ? $t('button.packUp') : $t('button.advance') }}
              <a-icon :type="advanced ? 'up' : 'down'"/>
            </a> -->
          </span>
        </a-row>
      </a-form>
    </a-card>
    
    <a-card :bordered="false" style="margin-top: 10px">
      <s-table
        v-if="initDone"
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        :alert="{show: false}"
        :pageSize="parameter.pageSize"
        :pageNum="parameter.pageNum"
        :scroll="{ x: 1600 }"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>

        <span slot="kkxqm" slot-scope="text, record">
          {{ record.kkxqm | filterKkxqm(TYPE0072) }}
        </span>

        <template slot="operation" slot-scope="a,row">
          <a v-if="row.sftksq == 0" @click="toApply(a)">{{ $t('key.tk') }}</a>
        </template>
      </s-table>
    </a-card>
  </div>
</template>

<script>
import { STable } from '~'
import { mapState } from 'vuex'
import searchMixin from '@/utils/searchRemain'

export default {
  mixins: [ searchMixin ],
  name: 'schedule',
  data() {
    return {
      advanced: true,
      queryParam: {},
      columns: [
        {
          title: this.$t('key.no'),
          width: '80px',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: this.$t('key.kcdm'),
          width: '120px',
          dataIndex: 'kcbm'
        },
        {
          title: this.$t('key.kcmc'),
          width: '120px',
          dataIndex: 'kcmc'
        },
        {
          title: this.$t('key.kkxn'),
          width: '120px',
          dataIndex: 'kkxn'
        },
        {
          title: this.$t('key.kkxq'),
          width: '120px',
          // dataIndex: 'kkxqm'
          scopedSlots: { customRender: 'kkxqm' }
        },
        {
          title: this.$t('key.rkjs'),
          width: '120px',
          dataIndex: 'jsmc'
        },
        {
          title: this.$t('key.skzlx'),
          width: '120px',
          dataIndex: 'skzlxmc'
        },
        {
          title: this.$t('key.skz'),
          width: '100px',
          dataIndex: 'skzmc'
        },
        {
          title: this.$t('key.kskj'),
          width: '100px',
          dataIndex: 'kskj'
        },
        {
          title: this.$t('key.jskj'),
          width: '100px',
          dataIndex: 'jskj'
        },
        {
          title: this.$t('key.ksz'),
          dataIndex: 'ksz',
          width: '100px',
        },
        {
          title: this.$t('key.jsz'),
          dataIndex: 'jsz',
          width: '100px',
        },
        {
          title: this.$t('key.jsmc'),
          dataIndex: 'classroomNo',
          width: '120px',
        },
        {
          title: this.$t('key.jswz'),
          dataIndex: 'location',
          width: '150px',
        },
        {
          title: this.$t('key.kclx'),
          dataIndex: 'kclbmc',
          width: '100px',
        },
        {
          title: this.$t('key.xf'),
          dataIndex: 'xf',
          width: '100px',
        },,
        {
          title: this.$t('key.option'),
          width: '120px',
          scopedSlots: { customRender: 'operation' }
        }
      ],
      parameter: {
        pageNo: 1,
        pageSize: 10
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        if(!this.initDone){
          return false;
        }
        this.parameter = parameter
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        // params.xkdm = params.yjxkm ? params.yjxkm : params.mlm
        return this.$api.cultivate.queryPkxxlist(params).then(res => {
          console.log(res)
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectMenu: {},
      detailVisible: false,
      confirmLoading: false,
      courseInfo: {},
      initDone: false
    }
  },
  components: { STable },
  computed: {
    ...mapState({
      TYPE0125: state => state.app['TYPE0125'], // 学年
      TYPE0072: state => state.app['TYPE0072'], // 学期
      TYPE0111: state => state.app['TYPE0111'], // 课程类别
      TYPE0359: state => state.app['TYPE0359']
    })
  },
  created () {
    this.$store.dispatch('app/setDictionary', 'TYPE0125')
    this.$store.dispatch('app/setDictionary', 'TYPE0072')
    this.$store.dispatch('app/setDictionary', 'TYPE0111')
    this.$store.dispatch('app/setDictionary', 'TYPE0359')
    // this.initSelectMenu()

  },
  methods: {
    initSelectMenu (notFirst = 0) {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
        if (!notFirst) {
          this.$store.dispatch('app/sysInfo').then(res => {
            this.$set(this.queryParam, 'kkxn', res.schoolYear)
            this.$set(this.queryParam, 'kkxqm', res.currentSemester)
            this.$nextTick(()=>{
              this.initDone = true
              this.$refs.table.refresh(true)
            })
          })
        }
      })
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    lbChange (code) {
      this.$set(this.queryParam, 'kcbh', '')
      if (code) {
        this.$api.cultivate.courseList({kcssyxm: code, kclbm: this.queryParam.kclb}).then(res => {
          this.$set(this.selectMenu, 'kc', res.data)
        })
      } else {
        this.$set(this.selectMenu, 'kc', [])
      }
    },
    handleExport () {
      this.$export('teacherClient/exportList',this.queryParam)
    },
    expoetSub(){
      this.$export('teacherClient/exportPdf',this.queryParam)
    },
    toApply(a) {
      this.$router.push({ name: '调课申请', query: { id: a.id }})
    }
  },
  filters: {
    filterKkxqm(value, type) {
      let name = null
      type && type.map(item => {
        if(item.code === value) return name = item.name
      })
      return name
    }
  }
}
</script>
